package zhishuang.wang.sansi.fcms.devinfor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import zhishuang.wang.sansi.tools.ReturnData;
import zhishuang.wang.sansi.tools.ToolClass;
import zhishuang.wang.sansi.tools.time.DateFormat;


/**
 * @Author Zhishuang.Wang
 * @Date 2023/7/5 10:52
 * @Email 1406110602@qq.com
 */
public class DeviceVar {

    public static ReentrantLock deviceInforMapLock = new ReentrantLock();
    public static Map<String, DeviceInfor> deviceInforMap = new HashMap();

    public DeviceVar() {
    }

    public static ReturnData deviceInforInit(String deviceId, int fcmsVersion, String ipAddr, int ipPort, int blockSize) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        if (blockSize > 511) {
            if (ToolClass.ipCheck(ipAddr)) {
                if (ipPort > 1024 && ipPort < 65535) {
                    deviceInforMapLock.lock();
                    switch (fcmsVersion) {
                        case 1:
                            DeviceInfor di = (DeviceInfor)deviceInforMap.get(deviceId);
                            if (di == null) {
                                di = new DeviceInfor();
                                di.setControFlag(0);
                                di.setFcmsVersion(fcmsVersion);
                                di.setIpAddr(ipAddr);
                                di.setIpPort(ipPort);
                                di.setOnlineFlag(1);
                                di.setBlockSize(blockSize);
                                deviceInforMap.put(deviceId, di);
                                rd.setCode(0);
                                rd.setMessage("添加成功");
                            } else {
                                rd.setMessage("设备唯一ID重复");
                            }
                            break;
                        default:
                            rd.setMessage("FCMS协议版本不支持，取值范围：1");
                    }

                    deviceInforMapLock.unlock();
                } else {
                    rd.setMessage("IP端口不合法，端口范围：1024 - 65535");
                }
            } else {
                rd.setMessage("IP地址不合法，格式：xxx.xxx.xxx.xxx，xxx取值返回：0-255");
            }
        } else {
            rd.setMessage("blockSize参数必须，具体值请和工程部或现场调试人员确认");
        }

        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData deviceInforDel(String deviceId) {
        ReturnData rd = new ReturnData();
        rd.setCode(1);
        deviceInforMapLock.lock();
        DeviceInfor di = (DeviceInfor)deviceInforMap.get(deviceId);
        if (di != null) {
            deviceInforMap.remove(deviceId);
            rd.setCode(0);
            rd.setMessage("删除成功");
        } else {
            rd.setMessage("设备信息不存在");
        }

        deviceInforMapLock.unlock();
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

    public static ReturnData deviceInforClear() {
        ReturnData rd = new ReturnData();
        deviceInforMapLock.lock();
        deviceInforMap.clear();
        deviceInforMapLock.unlock();
        rd.setCode(0);
        rd.setMessage("删除成功");
        rd.setTime(DateFormat.getNowDataFormat());
        return rd;
    }

}
